package com.hrm.dao.Impl;

import java.util.List;

import com.hrm.dao.JobDao;
import com.hrm.domain.Job;
import com.hrm.utils.JDBCTools;

/*
 * 职位数据处理层实现类
 */
@SuppressWarnings("all") // 使用注解抑制警告信息
public class JobDaoImpl implements JobDao {

	// 定义全局变量
	private Class jClass = Job.class;

	// 获取全部职业信息
	@Override
	public List<Job> getAllJob() {
		String sql = "select `ID` jID, `NAME` jName, `REMARK` jRemark from `job_inf`;";
		return JDBCTools.dataQuery(jClass, sql);
	}

	// 分页查询 + 模糊
	@Override
	public List<Job> getJobsByTerm(int flag, Object... parametes) {
		String sql = "";
		if (flag == 1) { // 没输入查询条件
			sql = "select `ID` jID, `NAME` jName, `REMARK` jRemark from `job_inf` limit ?,?;";
		} else {// 输入了查询条件
			sql = "select `ID` jID, `NAME` jName, `REMARK` jRemark from `job_inf` where NAME like ? limit ?,?;";
		}
		return JDBCTools.dataQuery(jClass, sql, parametes);
	}

	// 获取查询结果的记录条数
	@Override
	public Integer getCount(int flag, Object... parametes) {
		String sql = "";
		if (flag == 1) { // 没输入查询条件
			sql = "select count(*) from job_inf;";
		} else { // 输入了查询条件
			sql = "select count(*) from job_inf where NAME like ?;";
		}
		return JDBCTools.dataCount(sql, parametes);
	}

	// 添加职位
	@Override
	public Integer addJob(Job job) {
		String sql = "INSERT INTO `job_inf` (`NAME`, `REMARK`) VALUES (?, ?);";
		return JDBCTools.dataUpdate(sql, job.getjName(), job.getjRemark());
	}

	// 根据职位名称查找职位
	@Override
	public Job getJobByJN(String jobName) {
		String sql = "select `ID` jID, `NAME` jName, `REMARK` jRemark from `job_inf` where `NAME` = ?;";
		Job job = null;
		List<Job> jobs = JDBCTools.dataQuery(jClass, sql, jobName);
		if (!jobs.isEmpty()) {
			job = jobs.get(0);
		}
		return job;
	}

	// 根据职位ID获取指定职位
	@Override
	public Job getJobByID(int ID) {
		String sql = "select `ID` jID, `NAME` jName, `REMARK` jRemark from `job_inf` where `ID` = ?;";
		Job job = null;
		List<Job> jobs = JDBCTools.dataQuery(jClass, sql, ID);
		if (!jobs.isEmpty()) {
			job = jobs.get(0);
		}
		return job;
	}

	// 根据职位ID删除指定职位
	@Override
	public Integer delJob(int ID) {
		String sql = "delete from `job_inf` where `ID` = ?;";
		return JDBCTools.dataUpdate(sql, ID);
	}

	// 根据职位ID修改指定的职位信息
	@Override
	public Integer modifyJobByID(Job newJob, int ID) {
		String sql = "UPDATE `job_inf` SET `NAME` = ?, `REMARK` = ? WHERE `ID` = ?;";
		return JDBCTools.dataUpdate(sql, newJob.getjName(), newJob.getjRemark(), ID);
	}

}
